package petertel.pbx.conf.sip;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import petertel.pbx.conf.ConfigItem;
import petertel.pbx.conf.NameValuePair;

public class Device extends ConfigItem<NameValuePair> {

	private String name;

	private String template;

	@Override
	public String write() {
		return "[" + name + "]";
	}

	@Override
	public String toString() {
		String ret = "";
		ret += "[" + getLineno() + "][" + getFlag() + "] " + getName() + " "
				+ getTemplate();
		ret += "\n";
		for (int i = 0; i < children.size(); i++) {
			ret += children.get(i);
		}
		return ret;
	}

	public void updateProperty(int index, String propertyName,
			String propertyValue) {

	}

	public boolean isDirty() {
		for (NameValuePair prop : children) {
			if (prop.getFlag() != null) {
				return true;
			}
		}
		return false;
	}

	public Collection<Property> getCanSelectProperties() {
		Map<String, Property> canSelectPropertyMap = new HashMap<String, Property>();
		for (Property property : Device.Property.values()) {
			canSelectPropertyMap.put(property.toString(), property);
		}
		for (int i = 0; i < getChildren().size(); i++) {
			NameValuePair nameValuePair = getChildren().get(i);
			canSelectPropertyMap.remove(nameValuePair.getName());
		}
		return canSelectPropertyMap.values();
	}

	public boolean getCanInsertProperty() {
		int size = getCanSelectProperties().size();
		if (size > 0)
			return true;
		return false;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public boolean isTemplate() {
		return template != null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setProperties(List<NameValuePair> properties) {
		this.children = properties;
	}

	public static enum Property {
		type, defaultuser, secret, host, context, allowoverlap, udpbindaddr, tcpenable, tcpbindaddr, transport, srvlookup
	}

	public static enum SpecialDevice {
		general, authentication
	}

}
